package JPetStore.persistence.impl;

import JPetStore.domain.Sequence;
import JPetStore.persistence.DBUtil;
import JPetStore.persistence.SequenceDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SequenceDaoImpl implements SequenceDao {
    private static final String getSequenceString = "SELECT name, nextid FROM SEQUENCE WHERE NAME = ?";
    private static final String updateSequence = "UPDATE SEQUENCE SET NEXTID = ? WHERE NAME = ?";

    public SequenceDaoImpl() {
    }

    public Sequence getSequence(Sequence sequence) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement("SELECT name, nextid FROM SEQUENCE WHERE NAME = ?");
            ResultSet resultSet = pStatement.executeQuery();
            if (resultSet.next()) {
                sequence.setName(resultSet.getString(1));
                sequence.setNextId(resultSet.getInt(2));
            }

            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(pStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return sequence;
    }

    public void updateSequence(Sequence sequence) {
        Connection conn = null;
        PreparedStatement stm = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE SEQUENCE SET NEXTID = ? WHERE NAME = ?";
            stm = conn.prepareStatement(sql);
            stm.setString(1, sequence.getName());
            stm.setInt(2, sequence.getNextId());
        } catch (Exception var5) {
            var5.printStackTrace();
        }

    }
}
